package com.pritive.service.serviceImpl;

import com.pritive.bean.User;
import com.pritive.mapper.UserMapper;
import com.pritive.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> selectLike(String username) {
        InputStream is = null;
        SqlSession sqlSession = null;
        List<User> users = null;
        try {
             is = Resources.getResourceAsStream("MyBatisConfig.xml");
             sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
             UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
             users = userMapper.selectLike(username);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sqlSession!= null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return users;
    }
}
